function $getObj(id){
	return document.getElementById(id);
}
function $next(obj){
	return $('#'+obj.id).next();
}
var cst = {
    regex: {
		code: /^DYDS_[0-9]{4}/ ,
		name: /^[\u4E00-\u9FA5]{1,10}$/ ,
		certNo: /^[0-9a-zA-Z]{18}$/,
        username: /^[a-z0-9A-Z_]{2,16}$/,
        password:/^[0-9a-zA-Z\~\!\@\#\$%\^&\*\(\)\-\_\=\+\;\:\'\"\,\<\.\>\/\?\\]{4,30}$/,
		repassword: /^.{4,30}$/,
        mobile: /^1[3-9][0-9]{9}$/,
        email: /^[a-zA-Z0-9_+.-]+@([a-zA-Z0-9-]+\.)+[a-z0-9]{2,4}$/i,
		comment: /<[^>].*?>/i,
		age: /^[1-9]{1}[0-9]{0,2}$/	
    },
    msg: {
 
        unFormat: "\u8BF7\u9009\u62E9\u6709\u6548\u7684\u7528\u6237\u540D\uFF0C\u652F\u63012\u81F316\u4F4D\u82F1\u6587\u548c\u6570\u5B57",
        passNotice: "4\u81f330\u4f4d\u5b57\u7b26",
        passFormat: "\u8bf7\u8f93\u51654\u81f330\u4f4d\u5b57\u7b26\u4f5c\u4e3a\u5bc6\u7801",
        passIdent: "\u4E24\u6B21\u5BC6\u7801\u4E0D\u4E00\u81F4",
        mobileNote: "\u83B7\u53D6\u6FC0\u6D3B\u7801\u6FC0\u6D3B\u60A8\u7684\u5E10\u6237",
        emailNote: "\u83B7\u53D6\u6FC0\u6D3B\u7801\u6FC0\u6D3B\u60A8\u7684\u5E10\u6237",
        mobileFormat: "\u8BF7\u8F93\u5165\u6709\u6548\u7684\u624B\u673A\u53F7\u7801",
        emailFormat: "\u8BF7\u8F93\u5165\u6709\u6548\u7684\u90AE\u4EF6\u5730\u5740",
		noemptyFormat: "\u8bf7\u8f93\u5165\u6709\u6548\u7684\u5185\u5bb9",
		nameFormat: "\u4ec5\u4e3a\u6c49\u5b57\uff0c\u957f\u5ea6\u4e3a10\u4e2a\u5b57\u4ee5\u5185",
		identificationFormat: "\u8eab\u4efd\u8bc1\u53f7\u683c\u5f0f\u4e0d\u6b63\u786e",
		introFormat: "\u81ea\u6211\u4ecb\u7ecd\u4e0d\u80fd\u8d85\u8fc7500\u4e2a\u5b57\uff0c\u5e76\u4e14\u4e0d\u80fd\u51fa\u73b0html\u6807\u8bb0\uff0c\u5982&lta&gt&ltscript&gt",
    	enteridFormat: "\u62a5\u540d\u5e8f\u53f7\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u6b63\u786e\u683c\u5f0f: &quotDYDS_&quot+&quot\u56db\u4f4d\u968f\u673a\u6570\u5b57&quot",
		ageFormat: "\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u5e74\u9f84"
	}
};

var fields = (function () {
    var a = cst.msg;
	var re=cst.regex
    return {
		code: {
			note: "&nbsp;",
			yes: "&nbsp;",
			validations: [{
				test: function (b){
					return re.code.test(b.value);
				},
				msg: a.enteridFormat
			}]
		},
		name: {
			note: "&nbsp;",
			yes: "&nbsp;",
			validations: [{
				test: function (b){
					return re.name.test(b.value);
				},
				msg: a.nameFormat
			}]
		},
		certNo: {
			note: "&nbsp;",
			yes: "&nbsp;",
			validations: [{
				test: function (b){
					return re.certNo.test(b.value);
				},
				msg: a.identificationFormat
			}]
		},
		school: {
			note: "&nbsp;",
			yes: "&nbsp;",
			validations: [{
				test: function (b){
					if(b.value.trim()=="")
						return false;
					else return true;
				},
				msg: a.noemptyFormat
			}]
		},
		major: {
			note: "&nbsp;",
			yes: "&nbsp;",
			validations: [{
				test: function (b){
					if(b.value.trim()=="")
						return false;
					else return true;
				},
				msg: a.noemptyFormat
			}]
		},
		grade: {
			note: "&nbsp;",
			yes: "&nbsp;",
			validations: [{
				test: function (b){
					if(b.value.trim()=="")
						return false;
					else return true;
				},
				msg: a.noemptyFormat
			}]
		},
        username: {
            note: "&nbsp;",
            yes: "&nbsp;",
            validations: [{
                test: function (b) {
                    return re.username.test(b.value)
                },
                msg: a.unFormat
            }]
        },
        password: {
            note: "&nbsp;",
            yes: "&nbsp;",
            validations: [{
                test: function (b) {
                    return (re.password.test(b.value) && b.value.indexOf(" ") == -1)
                },
                msg: a.passFormat
            }]
        },
        repassword: {
            note: "&nbsp;",
            yes: "&nbsp;",
            validations: [{
                test: function (b) {
                    return $getObj('password').value == b.value
                },
                msg: a.passIdent
            }]
        },
        mobile: {
            note: "&nbsp;",
            yes: "&nbsp;",
            validations: [{
                test: function (b) {
                    return re.mobile.test(b.value)
                },
                msg: a.mobileFormat
            }]
        },
        email: {
            note: "&nbsp;",
            yes: "&nbsp;",
            validations: [{
                test: function (b) {
                    return re.email.test(b.value)
                },
                msg: a.emailFormat
            }]
        },
		comment:  {
            note: "&nbsp;",
            yes: "&nbsp;",
            validations: [{
                test: function (b) {
					if(b.value=="") return true;
                    return (b.value)
                },
                msg: a.introFormat
            }]
        },
		age: {
            note:"&nbsp;",
            yes: "&nbsp;",
            validations: [{
                test: function (b) {
					return re.age.test(b.value)
                },
                msg: a.ageFormat
            }]
        },
		hide_avatar:{
			note:null,
            yes: null,
            validations: [{
                test: function (b) {
					if(b.value==""){
						alert("\u8bf7\u4e0a\u4f20\u5934\u50cf");
						return false;
					}
					return true;
                },
                msg: null
            }]
		},
		agree: {
            note: null,
            yes: null,
            validations: [{
                test: function (b) {
                    return b.checked
                },
                msg: null
            }]
        }
        
    }
})();

var validate = function () { 
    var b = cst,
        a = fields,
        f = util,
        c = 0;
    for (e in a) {
        c++
    }
	var array1=["code","name","certNo","school","major","grade","mobile","email","username", "password", "repassword","comment","hide_avatar"];
	var array2=["code","name","certNo","school","major","grade","mobile","email","username", "password", "repassword","comment","agree","hide_avatar"];
	var agree=$getObj('agree');
	if(!agree){  
		var array= array1;
		var btn=$getObj('btn_edit');
		for(var i=0;i<array.length;i++){
			( function (j){
				var obj=$getObj(array[j]);
				obj.onkeypress= function (){
					f.clearErr(this, a[this.name].note, j);
				};	  
			
			})(i);
			
			( function (j){
				var obj=$getObj(array[j]);
				obj.onblur=  function () {
					if (this.value && this.value.length > 0) 
						f.checkItem(this, a[this.name],j);	
				}
			})(i);
			
   		}
		if(!btn) return false;
        btn.onclick = function () {
			for(var i=0;i<array.length;i++)
            	f.clearErr($getObj(array[i]), a[array[i]].note,i)
			var h = 0, j=0;
			for (j ; j< array.length; j++) {
				//alert($getObj(array[j]).name);
				h += f.checkItem($getObj(array[j]), a[array[j]],j)
			}
			if (h != c) {
				return false;	
			}
			$.post('edit.htm', {
				"mobile" : mobile,
				"email" : email,
				"password" : password,
				"repassword" : repassword,
				"comment" : comment,
				"avatar" : avatar
			}, function(resp) {
				if (resp.status==0) {
					alert(resp.data);
					window.location.href='profile.htm';
				} else {
					alert(resp.message);
				}
			});
		}
	}
	else{
		var array= array2;
		var btn=$getObj('btn_sign');
		for(var i=0;i<array.length;i++){
			
			( function (j){
				var obj=$getObj(array[j]);
				obj.onkeypress= function (){
					f.clearErr(this, a[this.name].note, j);
				};	  
			
			})(i);
			
			( function (j){
				var obj=$getObj(array[j]);
				obj.onblur=  function () {
					if (this.value && this.value.length > 0) 
						f.checkItem(this, a[this.name],j);	
				}
			})(i);

   		}
		if(!btn) return false;
        btn.onclick = function () {
			for(var i=0;i<array.length;i++)
            	f.clearErr($getObj(array[i]), a[array[i]].note,i)
			var h = 0, j=0;
			for (j ; j< array.length; j++) {
				//alert($getObj(array[j]).name);
				h += f.checkItem($getObj(array[j]), a[array[j]],j)
			}
			if (h != c) {
				alert("\u60a8\u6ca1\u6709\u540c\u610f\u4ee5\u4e0a\u6761\u6b3e");
				return false;	
			}
			$.post(
				 'sign.htm',
				 { 
					"code" : code,
					"name" : name,
					"certNo" : certNo,
					"school" : school,
					"major" : major,
					"grade" : grade,
					"mobile" : mobile,
					"email" : email,
					"username" : username,
					"password" : password,
					"repassword" : repassword,
					"comment" : comment,
					"avatar" : avatar
				}, 
				function(resp) {
					if (resp.status==0) {
						alert(resp.data);
						window.location.href='home.htm';
					} else {
						alert(resp.message);
					}
			});
		}
	}
};




var util = {
   
    clearErr: function (obj, note, spanIndex) {
		if(obj.name=="agree" || obj.name=="hide_avatar") return;
		document.getElementsByClassName('note')[spanIndex].innerHTML=note;
    },
    showErr: function (obj, err, spanIndex) {
        if(obj.name=="agree" || obj.name=="hide_avatar") return;
		document.getElementsByClassName('note')[spanIndex].innerHTML= err;
    },
    showOK: function (obj, yes, spanIndex) {
        if(obj.name=="agree" || obj.name=="hide_avatar") return;
		document.getElementsByClassName('note')[spanIndex].innerHTML = yes;
    },
    checkItem: function (obj, field, spanIndex) {
        var validations = field.validations;
        for (var i = 0;i< validations.length; ++i) {
            var c = validations[i].test(obj);
            if (null == c) {
                continue
            }
            if (false == c) {
                util.showErr(obj, validations[i].msg, spanIndex);
                return 0
            }
        }
        util.showOK(obj , field.yes, spanIndex);
        return 1
    }
};